package com.lk.sys.controller;

import com.lk.sys.common.vo.JsonResult;
import com.lk.sys.entity.SysUsr;
import com.lk.sys.service.SysUserService;
import lombok.experimental.Tolerate;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user/")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("doLogin")
    public JsonResult doLogin(String username, String password) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken();
        token.setUsername(username);
        token.setPassword(password.toCharArray());
        subject.login(token);
        return new JsonResult("login ok");
    }

    @RequestMapping("doUpdateObject")
    public JsonResult doUpdateObject(SysUsr entity, Integer[] roleIds) {
        sysUserService.updateObject(entity, roleIds);
        return new JsonResult("update ok");
    }
    /**
     * 修改用户按钮，将要修改的用户信息带到修改页面中
     * @author Lukang
     * @date 2022/1/6 下午10:15
     * @param id
     * @return com.lk.sys.common.vo.JsonResult
     */
    @RequestMapping("doFindObjectById")
    public JsonResult findObjectById(Long id) {
        return new JsonResult(sysUserService.findObjectById(id));
    }

    /**
     * 添加页面保存按钮
     * @author Lukang
     * @date 2022/1/6 下午10:12
     * @param entity
     * @param roleIds
     * @return com.lk.sys.common.vo.JsonResult
     */
    @RequestMapping("doSaveObject")
    public JsonResult doSaveObject(SysUsr entity, Integer[] roleIds) {
        sysUserService.saveObject(entity, roleIds);
        return new JsonResult("save ok");
    }
    /**
     * 启用禁用按钮功能实现
     * @author Lukang
     * @date 2022/1/5 下午10:26
     * @param id
     * @param valid
     * @return com.lk.sys.common.vo.JsonResult
     */
    @RequestMapping("doValidById")
    public JsonResult doValidById(Long id, Integer valid) {
        sysUserService.validById(id, valid);
        return new JsonResult("update ok");
    }
    /**
     * 当前页数据展示
     * @author Lukang
     * @date 2022/1/2 下午3:34
     * @param username
     * @param pageCurrent
     * @return com.lk.sys.common.vo.JsonResult
     */
    @RequestMapping("doFindPageObjects")
    public JsonResult doFindPageObjects(String username, Long pageCurrent) {
        return new JsonResult(sysUserService.findPageObjects(username, pageCurrent));
    }

}
